package com.ysw.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysw.dao.ShopMapper;
import com.ysw.entity.Shop;
import com.ysw.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;


    @Override
    public PageInfo<Shop> getPageInfo(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查找全部
        List<Shop> list = shopMapper.selectAll();
        //存储数据
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Shop> shopSelectAll() {
        return shopMapper.selectAll();
    }

    @Override
    public int addShop(Shop shop) {
        return shopMapper.insert(shop);
    }

    @Override
    public List<Shop> shopSelectByName(String type) {
        return shopMapper.findByNameContaining(type);
    }

    @Override
    public int deleteShop(Integer id) {
        return shopMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void resetAutoIncrement() throws Exception {
        shopMapper.resetAutoIncrement();
    }

    @Override
    public Shop shopSelectById(int id) {
        return shopMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateShop(Shop shop) {
        return shopMapper.updateByPrimaryKeySelective(shop);
    }

    @Override
    public int count() {
        return shopMapper.count();
    }

    @Override
    public void export(HttpServletResponse response) throws Exception {
        List<Shop> list = shopMapper.selectAll();
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
        String fileName = "商品列表"+ ".xlsx";
        fileName = new String(fileName.getBytes(), "ISO-8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName );
        EasyExcel.write(response.getOutputStream(), Shop.class)
                .autoCloseStream(Boolean.FALSE)
                .sheet("商品列表")
                .doWrite(list);



    }


}
